//
//  VisitorView.swift
//  SinaBlog
//
//  Created by 李超 on 16/3/20.
//  Copyright © 2016年 lichao. All rights reserved.
//

import UIKit
//设置代理
protocol VisitorViewDelegate:NSObjectProtocol{
    
    func visitorViewRegister()
    func visitorViewLogin()
    
}

class VisitorView: UIView {
    

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        prepareUI()
        
    }
    //设置代理
    weak var delegate:VisitorViewDelegate?
    //点击注册按钮触发
    func registerBtnAction(){
        
        delegate?.visitorViewRegister()
    }
    //点击登陆按钮触发
    func loginBtnAction(){
        
        delegate?.visitorViewLogin()
    }
    
    //重写init方法必须实现该方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置显示的信息
    func setVisitorViewInfo(imageName: String , message: String){
        
        iconView.image = UIImage(named: imageName)
        messageLable.text = message
        messageLable.sizeToFit()
        houseView.hidden = true 
        //将遮盖图置于底部
        sendSubviewToBack(coverView)
        
    }
    
    //首页的转盘旋转
    func startAnimationRatation(){
        
      let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        //退出该控制器时不移除动画
        animation.removedOnCompletion = false
        
        iconView.layer.addAnimation(animation, forKey: "homeRotation")
        
    }
    
    
    //添加控件,并添加自动约束
    private func prepareUI(){
        //设置背景颜色
        backgroundColor = UIColor(white: 237/255, alpha: 1.0)
      //添加子视图
        addSubview(iconView)
        addSubview(coverView)
        addSubview(houseView)
        addSubview(messageLable)
        addSubview(registerButton)
        addSubview(loginButton)
        //设置自动约束生效
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        messageLable.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        //设置约束条件
        //iconView的水平方向约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        //iconview的垂直方向的约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
        //设置coverView的水平方向的约束
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0))
        //距离顶部的约束
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0))
        //底部的约束
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        //房子水平方向的约束
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        //垂直方向的约束 Y
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //设置messagelable的约束
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 250))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        //设置registerButton的约束
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        //添加登陆按钮的约束
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 0))
        
        
        
    
    }
   
    //mark:懒加载
    //转盘
    private lazy var iconView:UIImageView = {
        let view = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
       return view
        
    }()
    //遮罩
    private lazy var coverView:UIImageView = {
        let cover = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return cover    
    }()
    
    //房子
    private lazy var houseView:UIImageView = {
     
        let house = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return house
        
    }()
    
    //文字标签
    private lazy var messageLable:UILabel = {
        let message = UILabel()
        message.text = "关注一些人,看看有上面惊喜"
        message.textColor = UIColor.darkGrayColor()
        //设置文字居中
        message.textAlignment = NSTextAlignment.Center
        //设置换行
        message.numberOfLines = 0
        //设置自适应
        message.sizeToFit()
        
        return message
    
    }()
    
    //注册按钮
    
    private lazy var registerButton:UIButton = {
    
       let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        var image = UIImage(named: "common_button_white_disable")
        
       // image = image?.stretchableImageWithLeftCapWidth(1, topCapHeight: 0)
        
        btn.setBackgroundImage(image, forState: UIControlState.Normal)
        btn.addTarget(self, action: "registerBtnAction", forControlEvents: UIControlEvents.TouchUpInside)
        
       return btn
    
    }()
    
    //登陆按钮
    private lazy var loginButton:UIButton = {
      let btn = UIButton()
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        var image = UIImage(named: "common_button_white_disable")
        
       // image = image?.stretchableImageWithLeftCapWidth(1, topCapHeight:0)
        btn.addTarget(self, action: "loginBtnAction", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setBackgroundImage(image, forState: UIControlState.Normal)
    
        return btn;
    
    
    }()
    
 
    
}
